package com.clinic.action.user;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.entity.gui.GuiAssistant;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.logic.user.AssistantLogic;
import com.clinic.util.CommonUtil;

public class AssistantAction extends UserAction {

	@Autowired
	private AssistantLogic assistantLogic;
	
	private static final long serialVersionUID = -5074498376087904787L;
	private GuiAssistant guiAssistant;
	private GuiPagination guiPagination;
	private List<GuiAssistant> guiAssistantList;
	private String term;
	private SortProperty sort;
	private int size;
	private int page;
	
	public String registration() {
		if(isDoctorLogin()) {
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String show(){
		if (isDoctorLogin() || (isAssistantLogin() && id == getLoginUser().getUserId())) {
			guiAssistant = assistantLogic.getAssistantDetailsById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String edit(){
		if(isDoctorLogin()) {
			guiAssistant  = assistantLogic.getAssistantDetailsById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String delete(){
		if(isDoctorLogin()) {
			guiAssistant = assistantLogic.getAssistantDetailsById(id);
			if(guiAssistant.isAvailable()) {
				assistantLogic.deleteAssistantDetailsById(id);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}	
		return ActionResult.AUTH_FAILED;
	}
	
	public String update(){
		if(isDoctorLogin()) {
			boolean isValidAssistantUpdate = assistantLogic.isValidAssistantUpdate(guiAssistant);
			if(!isValidAssistantUpdate) {
				addActionError("Failed to insert.Please fill all neccessory fields");
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiAssistant = assistantLogic.updateAssistant(guiAssistant); // catch guiAssistant object details returned from updateAssistant()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String insert() {
		if(isDoctorLogin()) {
			boolean isValidInsert = assistantLogic.isValidInsert(guiAssistant);
			if(!isValidInsert){
				addActionError("Failed to insert.Please fill all neccessory fields"); 
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiAssistant = assistantLogic.insertAssistant(guiAssistant); // catch guiAssistant object details returned from inserAssistant()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String list(){
		if(isDoctorLogin() || isAssistantLogin() || isPatientLogin()) {
			Index index = CommonUtil.getIndexBypage(page);
			if (StringUtils.isNotBlank(term) || sort != null) {
				guiAssistantList = assistantLogic.getAssistantListByTerm(term, sort.getColumn(), index);
				guiPagination = assistantLogic.getPaginationOfAssistantsByTerm(term, sort.getColumn(), page);
				size = guiPagination.getTotalCount();
			} else {
				guiAssistantList = assistantLogic.getAssistantList(index);
				guiPagination = assistantLogic.getPaginationOfAssistants(page);
				size = guiPagination.getTotalCount();
			}		
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;	
	}

	public GuiAssistant  getGuiAssistant () {
		return guiAssistant ;
	}

	public void setGuiAssistant(GuiAssistant guiAssistant ){
		this.guiAssistant  = guiAssistant ;
	}

	public List<GuiAssistant> getGuiAssistantList() {
		return guiAssistantList;
	}

	public void setGuiAssistantList(List<GuiAssistant> guiAssistantList) {
		this.guiAssistantList = guiAssistantList;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public SortProperty getSort() {
		return sort;
	}

	public void setSort(SortProperty sort) {
		this.sort = sort;
	}
	
	public void setSort(String sortProperty) {
		this.sort = SortProperty.getValue(sortProperty);
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	
}
